#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>

int main()
{
    int k = 0;
    k = fork();
    if (k < 0)
    {
        perror("fork failed!");
        exit(-1);
    }
    else if (k == 0)
    {
        /*子进程执行的代码*/
        printf("child is running!\n");
        printf("child k = %d , pid = %d , ppid = %d\n", k, getpid(), getppid());
        exit(0);
    }
    else
    {
        /*父进程执行的代码*/
        sleep(2); //父进程休眠两秒等待子进程结束,但是这种做法不好
        printf("parent is running!\n");
        printf("parent k = %d , pid = %d , ppid = %d\n", k, getpid(), getppid());
        exit(0);
    }
    return 0;
}